<template>
  <div class="home">
    <div class="header">
      <h1>欢迎来到管理系统</h1>
      <div class="user-info">
        <span>当前用户: {{ currentUser?.username }} ({{ currentUser?.role }})</span>
        <el-button @click="handleLogout" type="danger" size="small">退出登录</el-button>
      </div>
    </div>

    <div class="nav-cards">
      <PermissionGuard permission="user:read">
        <el-card class="nav-card" @click="$router.push('/users')">
          <div class="card-content">
            <el-icon size="40"><User /></el-icon>
            <h3>用户管理</h3>
            <p>管理系统用户</p>
          </div>
        </el-card>
      </PermissionGuard>

      <PermissionGuard permission="role:read">
        <el-card class="nav-card" @click="$router.push('/roles')">
          <div class="card-content">
            <el-icon size="40"><UserFilled /></el-icon>
            <h3>角色管理</h3>
            <p>管理用户角色</p>
          </div>
        </el-card>
      </PermissionGuard>

      <PermissionGuard :any-permissions="['system:config', 'system:log']">
        <el-card class="nav-card">
          <div class="card-content">
            <el-icon size="40"><Setting /></el-icon>
            <h3>系统设置</h3>
            <p>系统配置管理</p>
          </div>
        </el-card>
      </PermissionGuard>
    </div>

    <div class="demo-section">
      <h2>权限控制示例</h2>
      
      <div class="permission-examples">
        <el-card>
          <template #header>
            <span>按钮权限控制</span>
          </template>
          
          <PermissionGuard permission="user:create">
            <el-button type="primary">创建用户 (需要 user:create 权限)</el-button>
          </PermissionGuard>
          
          <PermissionGuard permission="user:delete">
            <el-button type="danger">删除用户 (需要 user:delete 权限)</el-button>
          </PermissionGuard>
          
          <PermissionGuard role="admin">
            <el-button type="warning">管理员专用按钮</el-button>
          </PermissionGuard>
        </el-card>

        <el-card>
          <template #header>
            <span>内容权限控制</span>
          </template>
          
          <PermissionGuard permission="system:config" show-fallback>
            <div class="protected-content">
              <p>这是只有拥有 system:config 权限的用户才能看到的内容</p>
            </div>
          </PermissionGuard>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { User, UserFilled, Setting } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { useAuth } from '@/composables/useAuth'
import PermissionGuard from '@/components/PermissionGuard.vue'

const router = useRouter()
const { currentUser, logout } = useAuth()

const handleLogout = () => {
  logout()
  ElMessage.success('已退出登录')
  router.push('/login')
}
</script>

<style scoped>
.home {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40px;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
}

.nav-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 40px;
}

.nav-card {
  cursor: pointer;
  transition: transform 0.2s;
}

.nav-card:hover {
  transform: translateY(-5px);
}

.card-content {
  text-align: center;
  padding: 20px;
}

.card-content h3 {
  margin: 15px 0 10px 0;
  color: #333;
}

.card-content p {
  color: #666;
  margin: 0;
}

.demo-section {
  margin-top: 40px;
}

.permission-examples {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.protected-content {
  background: #f0f9ff;
  padding: 15px;
  border-radius: 4px;
  border-left: 4px solid #409eff;
}
</style>